using System;
using System.Net;
using ICIA.ARojas.Silverlight.DataService;
using Didsoft.Silverlight.ViewModel.Data;
using ICIA.ARojas.Visual.Especialidad.ViewModel;
using ICIA.ARojas.Visual.Grado.ViewModel;
using Didsoft.Silverlight.ViewModel.Commanding.Contracts;
using ICIA.ARojas.Visual.Destino;
using Didsoft.Silverlight.ViewModel;
using System.Linq;
using System.Collections.ObjectModel;

namespace ICIA.ARojas.Visual.Persona.ViewModel
{
    public class VmPersona : DataModelBase<ICIA.ARojas.Silverlight.Entities.Persona>
    {
        private Didsoft.Silverlight.Controls.Data.ViewModelAutocompleteSearchBox aCSBGrado;

        public Didsoft.Silverlight.Controls.Data.ViewModelAutocompleteSearchBox ACSBGrado
        {
            get { return aCSBGrado; }
            set { aCSBGrado = value; aCSBGrado.OnSearch += new EventHandler<Didsoft.Silverlight.Controls.Data.SearchParamEventArgs>(aCSBGrado_OnSearch); }
        }

        void aCSBGrado_OnSearch(object sender, Didsoft.Silverlight.Controls.Data.SearchParamEventArgs e)
        {
            VmGrado vmgrado = new VmGrado();
            vmgrado.DataService.OnSearchByFilterCompleted += new EventHandler<SearchByFilterEventArgs<Silverlight.Entities.Grado>>(DataService_OnSearchByFilterCompleted);
            vmgrado.DataService.SearchByFilter(e.Param);
        }

        void DataService_OnSearchByFilterCompleted(object sender, SearchByFilterEventArgs<Silverlight.Entities.Grado> e)
        {
            aCSBGrado.NotifyOnSearchCompleted(e.ListResult);
        }

        private Didsoft.Silverlight.Controls.Data.ViewModelAutocompleteSearchBox aCSBEspecialidad;

        public Didsoft.Silverlight.Controls.Data.ViewModelAutocompleteSearchBox ACSBEspecialidad
        {
            get { return aCSBEspecialidad; }
            set { aCSBEspecialidad = value; aCSBEspecialidad.OnSearch += new EventHandler<Didsoft.Silverlight.Controls.Data.SearchParamEventArgs>(aCSBEspecialidad_OnSearch); }
        }

        void aCSBEspecialidad_OnSearch(object sender, Didsoft.Silverlight.Controls.Data.SearchParamEventArgs e)
        {
            VmEspecialidad vmespecialidad = new VmEspecialidad();
            vmespecialidad.DataService.OnSearchByFilterCompleted += new EventHandler<SearchByFilterEventArgs<Silverlight.Entities.Especialidad>>(DataService_OnSearchByFilterCompleted);
            vmespecialidad.DataService.SearchByFilter(e.Param);
        }

        void DataService_OnSearchByFilterCompleted(object sender, SearchByFilterEventArgs<Silverlight.Entities.Especialidad> e)
        {
            aCSBEspecialidad.NotifyOnSearchCompleted(e.ListResult);
        }


        public VmPersona()
        {
            base.SetDataService(new PersonaDataService());
            base.GetAll();
            base.DataService.OnEntityGetAllCompleted += new EventHandler<GetAllEntitiesEventArgs<Silverlight.Entities.Persona>>(DataService_OnEntityGetAllCompleted);

            CmdDestinos = new DelegateCommand<object>(CmdDestinos_Executed,CmdDestinos_CanExecute);

        }

        void DataService_OnEntityGetAllCompleted(object sender, GetAllEntitiesEventArgs<Silverlight.Entities.Persona> e)
        {
            constanteItems = e.ListResult;
        }


        #region comandos

        public ICommandEvent CmdDestinos { get; set; }

        bool CmdDestinos_CanExecute(object param)
        {

            return param != null;
        }

        void CmdDestinos_Executed(object param)
        {
            DestinoModal modal = new DestinoModal(this.SelectedItem);
        }



        #endregion

        System.Collections.ObjectModel.ObservableCollection<Silverlight.Entities.Persona> constanteItems;
        internal void BuscarPersonasEnDataGrid(string cad)
        {
            System.Collections.ObjectModel.ObservableCollection<Silverlight.Entities.Persona> tmpItems = constanteItems;
                        
                var result = (from i in tmpItems
                             where (i.Apellidos!=null &&  i.Apellidos.StartsWith(cad)) ||
                                   (i.Nombre!=null && i.Nombre.StartsWith(cad)) ||
                                   (i.CI!=null && i.CI.StartsWith(cad)) ||
                                   (i.Grado!=null && i.Grado.Nombre.StartsWith(cad))
                             select i);

                if(result.Count()>0)
                  Items = new ObservableCollection<Silverlight.Entities.Persona>(result);

            
            
        }
    }
}


